#!/bin/bash
# ref: https://github.com/Gisto/nwjs-shell-builder
DIR="$( cd "../$( dirname "$0" )" && pwd)"
PACKAGE_NAME="FLUX_Studio"

DOWNLOAD_SOURCE="http://192.168.16.202"
# colors
TXT_NORMAL="\e[1m"
TXT_INFO="\e[32m"
TXT_WARNING="\e[93m"
TXT_ERROR="\e[31m"
TXT_RESET="\e[0m"

#arg
REPLY=$1

$DIR/_tools/nwjs-shell-builder/nwjs-build.sh --clean
rm -r $DIR/lib
mkdir -p $DIR/lib
cd $DIR/_tools

while true; do
    if test -z "$REPLY"; then
    	read -p "==> Build for which OS? (Windows[W], OSX[O], Linux[L]) "
    fi
    case $REPLY in
        [Ww] )
            printf "\n";
            printf "${TXT_INFO} Download Resources for Windows x86 ${TXT_RESET}";
            printf "\n";
            # dl latest ghost
            wget $DOWNLOAD_SOURCE/ghost/windows/fluxghost-win-latest-x86.zip
            # dl latest sli3er
            wget $DOWNLOAD_SOURCE/slic3r/windows/slic3r-mswin-x86-1-2-9a-stable.zip

            mv $DIR/_tools/fluxghost-win-latest-x86.zip $DIR/lib/fluxghost-win-latest-x86.zip
            mv $DIR/_tools/slic3r-mswin-x86-1-2-9a-stable.zip $DIR/lib/slic3r-x86.zip

            unzip $DIR/lib/fluxghost-win-latest-x86.zip -d $DIR/lib/
            unzip $DIR/lib/slic3r-x86.zip -d $DIR/lib/
            mv $DIR/lib/fluxghost-win-latest-x86/flux_api $DIR/lib/flux_api
            rm -r $DIR/lib/slic3r-x86.zip $DIR/lib/fluxghost-win-latest-x86 $DIR/lib/fluxghost-win-latest-x86.zip

            find /var/www/web-panel/lib/ -name "__MACOSX" | xargs rm -r

            printf "\n";
            printf "${TXT_INFO} Packing for Windows x86 ${TXT_RESET}";
            printf "\n";
            # dl latest ghost and sli3er (for x86)
            $DIR/_tools/nwjs-shell-builder/nwjs-build.sh --src=$DIR/public --name="$PACKAGE_NAME" --win-icon=$DIR/public/icon.png --version="1.0.0" --nw=0.12.3 --target="2" --build
            mkdir -p $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x86/lib
            unzip $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX_Studio-$(date +%Y%m%d)-win-ia32.zip -d $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x86
            $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX_Studio-$(date +%Y%m%d)-win-ia32.zip
            mv -f $DIR/lib $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x86/
            cp $DIR/_tools/platform-script/windows-firewall.bat $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x86/
            cd $DIR/_tools/nwjs-shell-builder/TMP/output/
            zip -r -X ./fstudio-$(date +%Y%m%d)-win-x86.zip ./fstudio-$(date +%Y%m%d)-win-x86
            cd $DIR/_tools

            # clear
            rm -r $DIR/lib
            mkdir -p $DIR/lib

            # build x64 for windows
            printf "\n";
            printf "${TXT_INFO} Download Resources for Windows x64 ${TXT_RESET}";
            printf "\n";
            # dl latest ghost
            wget $DOWNLOAD_SOURCE/ghost/windows/fluxghost-win-latest-x64.zip
            # dl latest sli3er
            wget $DOWNLOAD_SOURCE/slic3r/windows/slic3r-mswin-x64-1-2-9a-stable.zip

            mv $DIR/_tools/fluxghost-win-latest-x64.zip $DIR/lib/fluxghost-win-latest-x64.zip
            mv $DIR/_tools/slic3r-mswin-x64-1-2-9a-stable.zip $DIR/lib/slic3r-x64.zip

            unzip $DIR/lib/fluxghost-win-latest-x64.zip -d $DIR/lib/
            unzip $DIR/lib/slic3r-x64.zip -d $DIR/lib/
            mv $DIR/lib/fluxghost-win-latest-x64/flux_api $DIR/lib/flux_api
            rm -r $DIR/lib/slic3r-x64.zip $DIR/lib/fluxghost-win-latest-x64 $DIR/lib/fluxghost-win-latest-x64.zip

            find /var/www/web-panel/lib/ -name "__MACOSX" | xargs rm -r

            printf "\n";
            printf "${TXT_INFO} Packing for Windows x64 ${TXT_RESET}";
            printf "\n";

            # dl latest ghost and sli3er (for x64)
            $DIR/_tools/nwjs-shell-builder/nwjs-build.sh --src=$DIR/public --name="$PACKAGE_NAME" --win-icon=$DIR/public/icon.png --version="1.0.0" --nw=0.12.3 --target="3" --build
            mkdir -p $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x64/lib
            unzip $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX_Studio-$(date +%Y%m%d)-win-x64.zip -d $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x64
            rm $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX_Studio-$(date +%Y%m%d)-win-x64.zip
            mv -f $DIR/lib $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x64/
            cp $DIR/_tools/platform-script/windows-firewall.bat $DIR/_tools/nwjs-shell-builder/TMP/output/fstudio-$(date +%Y%m%d)-win-x64/
            cd $DIR/_tools/nwjs-shell-builder/TMP/output/
            zip -r -X ./fstudio-$(date +%Y%m%d)-win-x64.zip ./fstudio-$(date +%Y%m%d)-win-x64
            cd $DIR/_tools
            break;;

        [Oo] )
            printf "\n";
            printf "${TXT_INFO} Download Resources ${TXT_RESET}";
            printf "\n";
            # dl latest ghost
            wget $DOWNLOAD_SOURCE/ghost/osx/fluxghost-osx-latest.zip
            # dl latest sli3er
            wget $DOWNLOAD_SOURCE/slic3r/osx/slic3r

            mv $DIR/_tools/fluxghost-osx-latest.zip $DIR/lib/fluxghost-osx-latest.zip
            mv $DIR/_tools/slic3r $DIR/lib/slic3r

            unzip $DIR/lib/fluxghost-osx-latest.zip -d $DIR/lib/
            mv $DIR/lib/fluxghost-osx-latest/flux_api $DIR/lib/flux_api
            rm -r $DIR/lib/fluxghost-osx-latest $DIR/lib/fluxghost-osx-latest.zip

            find /var/www/web-panel/lib/ -name "__MACOSX" | xargs rm -r

            printf "\n";
            printf "${TXT_INFO} Building as APP ${TXT_RESET}"
            printf "\n";
            $DIR/_tools/nwjs-shell-builder/nwjs-build.sh --src=$DIR/public --name="$PACKAGE_NAME" --osx-icon=$DIR/public/logo.icns --version="1.0.0" --nw=0.12.3 --target="5" --build

            unzip $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX_Studio-$(date +%Y%m%d)-osx-x64.zip -d $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio
            mv $DIR/lib $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/FLUX_Studio.app/Contents

            # build as a dmg
            mkdir -p $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio
            printf "\n";
            printf "${TXT_INFO} Packing a dmg ${TXT_RESET}"
            printf "\n";
            mv $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/FLUX_Studio.app $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/FLUX\ Studio.app
            # Mac OSX
            if [[ "$OSTYPE" == "darwin"* ]]; then
                  ln -s /Applications $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/Applications
                  hdiutil create -size 500m -srcfolder $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/ $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/fstudio-$(date +%Y%m%d)-osx.dmg
                  rm $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/Applications
            else
                  zip -r -X ~/fstudio-$(date +%Y%m%d)-osx.zip $DIR/_tools/nwjs-shell-builder/TMP/output/FLUX\ Studio/FLUX\ Studio.app
                  cp ~/fstudio-$(date +%Y%m%d)-osx.zip /var/www/latest-release/app/fstudio-$(date +%Y%m%d)-osx.zip
            fi
            break;;

        [Ll] )
            printf "\n";
            printf "${TXT_INFO} Download Resources for Linux ${TXT_RESET}";
            printf "\n";
            # dl latest ghost
            wget $DOWNLOAD_SOURCE/ghost/linux/fluxghost-linux-latest.zip
            # dl latest sli3er
            wget $DOWNLOAD_SOURCE/slic3r/linux/slic3r-linux-x86_64-1-2-9-stable.tar.gz

            mv $DIR/_tools/fluxghost-linux-latest.zip $DIR/public/lib/fluxghost-linux-latest.zip
            mv $DIR/_tools/slic3r-linux-x86_64-1-2-9-stable.tar.gz $DIR/public/lib/slic3r-linux-latest.tar.gz

            unzip $DIR/public/lib/slic3r-linux-latest.tar.gz -d $DIR/public/lib/
            mv $DIR/public/lib/fluxghost-linux-latest/ghost $DIR/public/lib/ghost
            rm -r $DIR/public/lib/fluxghost-linux-latest $DIR/public/lib/fluxghost-linux-latest.zip

            find /var/www/web-panel/public/lib/ -name "__MACOSX" | xargs rm -r

            printf "\n";
            printf "${TXT_INFO} Packing for Linux ${TXT_RESET}";
            printf "\n";
            ./nwjs-shell-builder/nwjs-build.sh --src=$DIR/public --name="$PACKAGE_NAME" --osx-icon=$DIR/public/logo.icns --version="1.0.0" --nw=0.12.3 --target="0 1" --build

            break;;

        * )
            echo -e "Please chooce the specific OS";
    esac
done

printf "\n${TXT_INFO} Done ${TXT_RESET}\n"
open $DIR/_tools/nwjs-shell-builder/TMP/output/
exit 0;
